﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Annuaire;
using annuairedotnet;

public partial class AffContactPro : System.Web.UI.Page
{
    Contact contactFromRequest;
    int idFromRequest = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Roles.IsUserInRole("user"))
        {
            panel.Enabled = false;
            btEnregistrer.Visible = false;
        }
        // Enregistre l url référente pour la fonction de retour
        ViewState["UrlReferent"] = Request.UrlReferrer.ToString();

        idFromRequest = ((Contact)Session["contact"]).Id;
        contactFromRequest = ContactService.getById(idFromRequest);
         if (!Page.IsPostBack){

             if (idFromRequest != 0)
             {
                 tbSociete.Text = contactFromRequest.DonneesPro.Societe;
                 tbProfession.Text = contactFromRequest.DonneesPro.Profession;
                 tbAdresse.Text = contactFromRequest.DonneesPro.AdressePro;
                 tbCp.Text = contactFromRequest.DonneesPro.CpPro;
                 tbVille.Text = contactFromRequest.DonneesPro.VillePro;
                 tbTelephone.Text = contactFromRequest.DonneesPro.TelephonePro;
                 tbTelecopie.Text = contactFromRequest.DonneesPro.Telecopie;
                 tbEmail.Text = contactFromRequest.DonneesPro.EmailPro;
             }
         }
   }

    protected void btPrecedent_Click(object sender, EventArgs e)
    {
        Server.Transfer("AffContactPerso.aspx");  
    }

    protected void btAnnuler_Click(object sender, EventArgs e)
    {
        if (idFromRequest != 0)
        {
            Session.Add("urlReferent", "AffContactPro.aspx");
        }
        else
        {
            Session.Remove("urlReferent");
        }
        
        Server.Transfer("RechContact.aspx");
    }
    protected void btEnregistrer_Click(object sender, EventArgs e)
    {
        Contact contact = (Contact)Session["contact"];

        if (cbSansProfession.Checked == false)
        {
            DonneesPro donneesPro = new DonneesPro();

            donneesPro.Societe = tbSociete.Text;
            donneesPro.Profession = tbProfession.Text;
            donneesPro.AdressePro = tbAdresse.Text;
            donneesPro.CpPro = tbCp.Text;
            donneesPro.VillePro = tbVille.Text;
            donneesPro.TelephonePro = tbTelephone.Text;
            donneesPro.Telecopie = tbTelecopie.Text;
            donneesPro.EmailPro = tbEmail.Text;

            contact.DonneesPro = donneesPro;
        }

        contact.DonneesPerso.Photo = (string)Session["urlPhoto"];
        ContactService.saveContact(contact);
 
        Server.Transfer("InsertUpdateConfirm.aspx");
    }
    protected void btEmail_Click(object sender, ImageClickEventArgs e)
    {
        Session.Add("mail", tbEmail.Text);
        btEmail.Attributes.Add("onclick", "window.open('EnvoiMail.aspx',null,'left=300, top=100, height=625, width= 900, status=n o, resizable= no, scrollbars= no, toolbar= no,location= no, menubar= no');");
    }
}
